package org.batatasframework.support.charts;

import java.text.DecimalFormat;
import java.util.Map;

/**
 * 把读取到的数据解析成图形统计界面所需要的格式
 * @author jackson
 *
 */
@Deprecated
public class ResultDataParser {
	
	public static final ResultDataParser decimalReader(DecimalFormat format){
		return new ResultDataParser(ResultDataCell.cellDecimal(format));
	}

	public static final ResultDataParser decimalReader(){
		return new ResultDataParser(ResultDataCell.cellDecimal());
	}

	public static final ResultDataParser numberReader(DecimalFormat format){
		return new ResultDataParser(ResultDataCell.cellNumber(format));
	}

	public static final ResultDataParser numberReader(){
		return new ResultDataParser(ResultDataCell.cellNumber());
	}

	private final ResultDataCell dataCell;
	private int nameIndex = 0;
	
	public void set(int nameIndex, int valueIndex){
		this.nameIndex = nameIndex;
		this.dataCell.setValueIndex(valueIndex);
	}
	
	private ResultDataParser(ResultDataCell dataCell){
		this.dataCell = dataCell;
	}

	public String readName(Object[] its) {
		return its[nameIndex].toString();
	}
	
	/** 这里主要是进行数据的格式化处理，如果需要的话。 */
	public Object[] dataParse(Map<String, Number> sum){
		if(dataCell.needFormat()){
			int i = 0;
			Object[] rs = new Object[sum.size()];
			for(Number num : sum.values()){
				rs[i++] = dataCell.format(num);
			}
			return rs;
		}
		
		return sum.values().toArray();
	}
	
	public Number readValue(Object[] its, Map<String, Number> sum) {
		String name = readName(its);
		Number sor = dataCell.readValue(its);
		Number val = sum.get(name);

		val = dataCell.readValue(sor, val);
		
		sum.put(name, val);
		return val;
	}
}
